<?php
class Supplier_account_logAction extends CommonAction {
	
	public function index(){
		$supplier_id = $this->_get('supplier_id');
		//$this->listorder="dateline desc";
		$this->supplier_id = $supplier_id;
        $pagesize = 10;
        if ($this->_get('pagesize')) {
            $pagesize = $this->_get('pagesize');
        }
        //通过供货商id，查找对应的会员id，再找到已经完成的订单
        $Model = new Model();
        $orderDB=D('supplier_account_log');
        $listmap = 's.site_id = '.$this->siteid.' and s.supplier_id = '.$supplier_id;//站点、商品属于该站点


        //实例化分页类
        import('ORG.Util.Page');

        $count = $orderDB->alias('s')->where($listmap)->count();
        
        $p = new Page($count, $pagesize);
        $list = $orderDB->alias('s')->where($listmap)->limit($p->firstRow . ',' . $p->listRows)->select();// member 可用

		$pages = $p->show();
		$this->assign('list', $list);
		$this->assign('pages', $pages);
		$this->display();
	}
	
	public function add(){
		$this->supplier_id = $this->_get('supplier_id');
		parent::add();
	}

	
	public function insert(){
		$site_id=$this->siteid;
		$chanageType = $this->_post('type');
		$memberDb = D('supplier_account_log');
		if($chanageType == 'setInc'){
			$data['changenum'] = $_POST['money'];
		}else{
			$data['changenum'] = $_POST['money'] * -1;
		}
		$data['site_id']=$this->siteid;
		$data['user_id'] = session(C('USER_AUTH_KEY') );
		$data['supplier_id'] = $this->_post('supplier_id');
		$data['remark'] = $_POST['remark'];
		$data['dateline'] = time();
		
		$ret = D('supplier_account_log')-> data($data) -> add();
        if($ret){
            //判断supplier_id是否有对应的member_id，如果有，往供货商账户member_account_log，写入金额，备注'供货商结算'
            $sup=D('member_supplier')->where(array('supplier_id'=>$data['supplier_id'],'site_id'=>$site_id))->find();
            if($sup){
                $member['site_id']      =$site_id;//网点
                $member['member_id']    = $sup['member_id'];//会员号
                $member['datetime']     =time();//时间
                $member['remark']       ='供货商结算';//备注
                $member['changenum']    =$data['changenum'];//金额
                $memberAdd=D('member_account_log')->add($member);//member_account_log 表添加一条数据
                if($memberAdd){									//往member表写入数据！！！
                    $oldmoney=D('member')->where(array('id'=>$member['member_id']))->getField('money');
                    $default['money']=$member['changenum']+$oldmoney;
                    $count=D('member')->where(array('id'=>$member['member_id']))->save($default);
                    if($count){
                        $this->success('操作成功！', U('index', array('supplier_id' => $this->_post('supplier_id'))));
                    }else{
                        $this->error(D('supplier_account_log')-> getError());
                    }
                }else{
                    $this->error(D('supplier_account_log')-> getError());
                }
            }else{
				$this->success('操作成功！', U('index', array('supplier_id' => $this->_post('supplier_id'))));
            }
        }else{
            $this->error(D('supplier_account_log')-> getError());
        }
	}
	
}
